package com.ruoyi.project.manage.sixDone.service.impl;

import java.util.List;

import com.ruoyi.common.constant.ProjectConstants;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.manage.approveLog.domain.ApproveLog;
import com.ruoyi.project.manage.approveLog.mapper.ApproveLogMapper;
import com.ruoyi.project.manage.invest.domain.ProjectInvest;
import com.ruoyi.project.manage.project.domain.Project;
import com.ruoyi.project.manage.project.mapper.ProjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.manage.sixDone.mapper.ProjectSixDoneMapper;
import com.ruoyi.project.manage.sixDone.domain.ProjectSixDone;
import com.ruoyi.project.manage.sixDone.service.IProjectSixDoneService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 项目六定记录Service业务层处理
 * 
 * @author fangkun
 * @date 2022-03-28
 */
@Service
public class ProjectSixDoneServiceImpl implements IProjectSixDoneService 
{
    @Autowired
    private ProjectSixDoneMapper projectSixDoneMapper;
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private ApproveLogMapper approveLogMapper;

    /**
     * 查询项目六定记录
     * 
     * @param projectSixId 项目六定记录主键
     * @return 项目六定记录
     */
    @Override
    public ProjectSixDone selectProjectSixDoneByProjectSixId(Long projectSixId)
    {
        return projectSixDoneMapper.selectProjectSixDoneByProjectSixId(projectSixId);
    }

    /**
     * 查询项目六定记录列表
     * 
     * @param projectSixDone 项目六定记录
     * @return 项目六定记录
     */
    @Override
    public List<ProjectSixDone> selectProjectSixDoneList(ProjectSixDone projectSixDone)
    {
        return projectSixDoneMapper.selectProjectSixDoneList(projectSixDone);
    }

    /**
     * 新增项目六定记录
     * 
     * @param projectSixDone 项目六定记录
     * @return 结果
     */
    @Override
    public int insertProjectSixDone(ProjectSixDone projectSixDone)
    {
        return 0;
    }

    /**
     * 修改项目六定记录
     * 
     * @param projectSixDone 项目六定记录
     * @return 结果
     */
    @Override
    public int updateProjectSixDone(ProjectSixDone projectSixDone)
    {
        Project project = new Project();
        project.setProjectId(projectSixDone.getProjectId());
        project.setInvestStatus(ProjectConstants.APPROVAL_UNSUBMIT);

        project.setProjectMeasure(projectSixDone.getProjectMeasure());
        project.setProjectDirector(projectSixDone.getProjectDirector());
        project.setResponseName(projectSixDone.getResponseName());
        project.setResponsePhone(projectSixDone.getResponsePhone());
        project.setPlanInvestQuarter1(projectSixDone.getPlanInvestQuarter1());
        project.setPlanInvestQuarter2(projectSixDone.getPlanInvestQuarter2());
        project.setPlanInvestQuarter3(projectSixDone.getPlanInvestQuarter3());
        project.setPlanInvestQuarter4(projectSixDone.getPlanInvestQuarter4());

        project.setUpdateTime(DateUtils.getNowDate());
        project.setUpdateBy(ShiroUtils.getSysUser().getUserName());

        return projectMapper.updateProject(project);
    }

    @Override
    public int submit(int projectId) {
        Project project = projectMapper.selectProjectByProjectId(Long.valueOf(projectId));

        if (project == null) {
            throw new ServiceException("指定项目不存在！");
        }

        if (ProjectConstants.APPROVAL_AGREE == project.getSixDoneStatus() || ProjectConstants.APPROVAL_SUBMIT == project.getSixDoneStatus()) {
            throw new ServiceException("指定项目六定已经提交！");
        }

        // 审批流转记录
        ApproveLog approveLog = new ApproveLog();
        approveLog.setEntityType(ProjectConstants.APPROVAL_TYPE_SIXDONE);
        approveLog.setEntityId(project.getProjectId());
        approveLog.setApproveResult(ProjectConstants.APPROVAL_SUBMIT);
        approveLog.setRemark("发起审核");
        approveLog.setCreateTime(DateUtils.getNowDate());
        approveLog.setCreateBy("录入员:" + ShiroUtils.getSysUser().getUserName());
        approveLog.setUpdateTime(DateUtils.getNowDate());
        approveLog.setUpdateBy(ShiroUtils.getLoginName());
        approveLogMapper.insertApproveLog(approveLog);

        project.setSixDoneStatus(ProjectConstants.APPROVAL_SUBMIT);
        project.setUpdateTime(DateUtils.getNowDate());
        project.setUpdateBy(ShiroUtils.getLoginName());
        return projectMapper.updateProject(project);
    }

    @Override
    public int withdraw(int projectId) {
        Project project = projectMapper.selectProjectByProjectId(Long.valueOf(projectId));

        if (project == null) {
            throw new ServiceException("指定项目不存在！");
        }

        if (ProjectConstants.APPROVAL_SUBMIT != project.getSixDoneStatus()) {
            throw new ServiceException("指定项目六定状态不能撤回！");
        }

        // 审批流转记录
        ApproveLog approveLog = new ApproveLog();
        approveLog.setEntityType(ProjectConstants.APPROVAL_TYPE_SIXDONE);
        approveLog.setEntityId(project.getProjectId());
        approveLog.setApproveResult(ProjectConstants.APPROVAL_UNSUBMIT);
        approveLog.setRemark("撤回");
        approveLog.setCreateTime(DateUtils.getNowDate());
        approveLog.setCreateBy("录入员:" + ShiroUtils.getSysUser().getUserName());
        approveLog.setUpdateTime(DateUtils.getNowDate());
        approveLog.setUpdateBy(ShiroUtils.getLoginName());
        approveLogMapper.insertApproveLog(approveLog);

        project.setSixDoneStatus(ProjectConstants.APPROVAL_UNSUBMIT);
        project.setUpdateTime(DateUtils.getNowDate());
        project.setUpdateBy(ShiroUtils.getLoginName());
        return projectMapper.updateProject(project);
    }

    @Override
    public int reject(Project project) {
        Project projectOld = projectMapper.selectProjectByProjectId(project.getProjectId());

        if (projectOld == null) {
            throw new ServiceException("指定项目不存在！");
        }

        if (ProjectConstants.APPROVAL_SUBMIT != projectOld.getSixDoneStatus()) {
            throw new ServiceException("指定项目六定不是上报状态！");
        }

        // 审批流转记录
        ApproveLog approveLog = new ApproveLog();
        approveLog.setEntityType(ProjectConstants.APPROVAL_TYPE_SIXDONE);
        approveLog.setEntityId(project.getProjectId());
        approveLog.setApproveResult(ProjectConstants.APPROVAL_REJECT);
        approveLog.setRemark("驳回");
        approveLog.setApproveComment(project.getRejectReason());
        approveLog.setCreateTime(DateUtils.getNowDate());
        approveLog.setCreateBy("审核员:" + ShiroUtils.getSysUser().getUserName());
        approveLog.setUpdateTime(DateUtils.getNowDate());
        approveLog.setUpdateBy(ShiroUtils.getLoginName());
        approveLogMapper.insertApproveLog(approveLog);

        project.setSixDoneStatus(ProjectConstants.APPROVAL_REJECT);
        project.setUpdateTime(DateUtils.getNowDate());
        project.setUpdateBy(ShiroUtils.getLoginName());
        return projectMapper.updateProject(project);
    }

    @Override
    public int agree(int projectId) {
        Project project = projectMapper.selectProjectByProjectId(Long.valueOf(projectId));

        if (project == null) {
            throw new ServiceException("指定项目不存在！");
        }

        if (ProjectConstants.APPROVAL_SUBMIT != project.getSixDoneStatus()) {
            throw new ServiceException("指定项目六定状态不能入库！");
        }

        // 审批流转记录
        ApproveLog approveLog = new ApproveLog();
        approveLog.setEntityType(ProjectConstants.APPROVAL_TYPE_SIXDONE);
        approveLog.setEntityId(project.getProjectId());
        approveLog.setApproveResult(ProjectConstants.APPROVAL_AGREE);
        approveLog.setRemark("审核通过");
        approveLog.setCreateTime(DateUtils.getNowDate());
        approveLog.setCreateBy("审核员:" + ShiroUtils.getSysUser().getUserName());
        approveLog.setUpdateTime(DateUtils.getNowDate());
        approveLog.setUpdateBy(ShiroUtils.getLoginName());
        approveLogMapper.insertApproveLog(approveLog);

        // 插入六定记录
        ProjectSixDone projectSixDone = new ProjectSixDone();
        projectSixDone.setProjectId(project.getProjectId());

        projectSixDone.setProjectMeasure(project.getProjectMeasure());
        projectSixDone.setProjectDirector(project.getProjectDirector());
        projectSixDone.setResponseName(project.getResponseName());
        projectSixDone.setResponsePhone(project.getResponsePhone());
        projectSixDone.setPlanInvestQuarter1(project.getPlanInvestQuarter1());
        projectSixDone.setPlanInvestQuarter2(project.getPlanInvestQuarter2());
        projectSixDone.setPlanInvestQuarter3(project.getPlanInvestQuarter3());
        projectSixDone.setPlanInvestQuarter4(project.getPlanInvestQuarter4());


        projectSixDone.setSixDoneStatus(ProjectConstants.APPROVAL_AGREE);
        projectSixDone.setStatus(UserConstants.NORMAL);
        projectSixDone.setCreateTime(DateUtils.getNowDate());
        projectSixDone.setCreateBy(ShiroUtils.getSysUser().getUserName());
        projectSixDone.setUpdateTime(DateUtils.getNowDate());
        projectSixDone.setUpdateBy(ShiroUtils.getSysUser().getUserName());
        projectSixDoneMapper.insertProjectSixDone(projectSixDone);

        // 设置资金状态
        project.setSixDoneStatus(ProjectConstants.APPROVAL_AGREE);
        project.setUpdateTime(DateUtils.getNowDate());
        project.setUpdateBy(ShiroUtils.getLoginName());
        return projectMapper.updateProject(project);
    }

    /**
     * 批量删除项目六定记录
     * 
     * @param projectSixIds 需要删除的项目六定记录主键
     * @return 结果
     */
    @Override
    public int deleteProjectSixDoneByProjectSixIds(String projectSixIds)
    {
        return projectSixDoneMapper.deleteProjectSixDoneByProjectSixIds(Convert.toStrArray(projectSixIds));
    }

    /**
     * 删除项目六定记录信息
     * 
     * @param projectSixId 项目六定记录主键
     * @return 结果
     */
    @Override
    public int deleteProjectSixDoneByProjectSixId(Long projectSixId)
    {
        return projectSixDoneMapper.deleteProjectSixDoneByProjectSixId(projectSixId);
    }
}
